/*
 * Copyright (C) 2009 IMOBISOFT Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package imobisoft.android.hialarm.application;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.net.Uri;
import android.preference.ListPreference;
import android.provider.MediaStore;
import android.util.AttributeSet;
import android.util.Log;

import imobisoft.android.hialarm.HiAlarm;
import imobisoft.android.hialarm.base.AlarmConst;

/**
 * 
 * Screen for set alarm ringtone
 *
 */
public class AlarmPreference extends ListPreference implements AlarmConst.IAlarmRingtone{

	private Cursor mCursor = null;
	private int mRingtoneIndex = 0;
	
	private OnClickListener mOnClickListener = new OnClickListener(){
		@Override
		public void onClick(DialogInterface dialog, int which) {
			Log.d(AlarmConst.TAG,Integer.toString(which));
			mRingtoneIndex = which;
		}
	};
	
	public AlarmPreference(Context context) {
		super(context); 
	}

	public AlarmPreference(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onDialogClosed(boolean positiveResult) {
		AlarmConst.getRingtoneUri(mCursor,this,mRingtoneIndex);
	}
	
	public void setCursor(Cursor cursor){
		mCursor = cursor;
		AlarmConst.buildRingtoneList(mCursor);
		AlarmConst.getRingtoneIndex(mCursor, this, Uri.parse(HiAlarm.getConfig().getDefaultRingtone()));
	}

	@Override
	protected void onPrepareDialogBuilder(Builder builder) {
		builder.setSingleChoiceItems(mCursor, mRingtoneIndex, MediaStore.MediaColumns.DISPLAY_NAME, mOnClickListener);
	}
	

	@Override
	public void reportRingtoneUri(Uri uri) {
		if(null != uri){
			HiAlarm.getConfig().setDefaultRingtone(uri.toString());
		}
	}

	@Override
	public void reportRingtoneIndex(int index) {
		mRingtoneIndex = index;
	}
	
}